// pages/worldup/worldup.js

const app = getApp()
var api = app.api;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentDate: "",
    overTimeDataList: [],
    matchData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.getWorldCup();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log("...下拉刷新...")
    if (this.data.overTimeDataList.length > 0) {
      let newArray = this.data.overTimeDataList.concat(this.data.matchData)
      this.setData({
        matchData: newArray,
        overTimeDataList: []
      })
    } else {
      //请求服务器
      this.getWorldCup();
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getWorldCup() {
    var that=this;
    api.GetWorldCup(resp => {
      const {
        data
      } = resp;
      console.log("文章列表==", data)
      this.setData({
        matchData: data
      })
      that.formatData()
    })
  },

  formatData(){
    var myDate = new Date();
    let month = myDate.getMonth()
    let day = myDate.getDate()
    var currentDayStr = (month + 1) + "月" + day + "日"
    this.setData({
      currentDate: currentDayStr
    })

    let overTimeList = []
    let slpIndex = 0;
    for (var i = 0; i < this.data.matchData.length; i++) {
      var tempData = this.data.matchData[i]
      if (tempData.date.startsWith(currentDayStr)) {
        console.log("~~~~~~~~~~~~~~~~~~~~~~")
        console.log(tempData.date)
        slpIndex = i;
        break
      } else {
        overTimeList.push(tempData);
      }
    }
    if (slpIndex > 0) {
      let newArray = this.data.matchData.slice(slpIndex)
      this.setData({
        matchData: newArray
      })
    }
    if (overTimeList.length > 0) {
      this.setData({
        overTimeDataList: overTimeList
      })
    }
  }
})